import Vue from 'vue'
import Vuex from 'vuex'
import { Ajax } from '@/api'

Vue.use(Vuex)

import VuexPersistedstate from 'vuex-persistedstate'

export default new Vuex.Store({
  plugins:[
    new VuexPersistedstate()
  ],
  state: {
    userInfo : null, //用户信息
    myCarts:[] , // 我的购物车数据
    goodList:[], //商品数据

  },
  getters: {
//     allTypes(state){
//       var arr = []
//       var obj = {}
//       var res = state.goodList.data.vo
//       res.forEach((item,index)=>{
//          if(!obj[item.typeName]){
//             obj[item.typeName] = item.typeName;
//             arr.push(item)
//          } 
//       })
//       return arr;
//  }

   
  },
  mutations: {
    changeUserInfo(state,payload){
      state.userInfo = payload;
     },
    changeMyCarts(state,payload){
      state.myCarts = payload;
     },
    changeGoods(state,payload){
      state.goodList = payload;
    },

     
  },
  actions: {
    async getMyCarts({commit},payload){
      let res = await Ajax.getCarts(payload)  // phone 
      commit("changeMyCarts",res);
   },
   async getAllGoods({commit},payload){
    let res = await Ajax.getGoods()
    commit("changeGoods",res)
  },
  
  },
  modules: {
  }
})
